package integration;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import blackjack.MyMath;

public class apple_mod_correct extends JPanel {
	
	private JButton b[]={ new JButton("hit"), new JButton ("stand"), new JButton("start")};
	private JTextArea area,area_score;
	private JTextField field;
	
	private int player;
	private int x;
	private int cpu;
	private int state_player;
	private int state_cpu;
	
	// CORRECTION: necessario per l'eventuale correzione del valore di 'A'
	private int cpu_aces;		// numero di 'A' non svalutati di cpu
	
	private boolean out=true;
	
	public apple_mod_correct() {
		area=new JTextArea();
		field=new JTextField();
		area_score=new JTextArea();
		add(field); 
		add(area_score);
		add(area);
		setLayout(null);
		setBackground(Color.BLACK);
		setBounds(100, 80, 520, 300);
		
		for (int i=0,y=0;i<3;i++,y+=20){
			b[i].addActionListener(new ascoltatore());
			b[i].setBounds(20,20+y,100,20);
		add(b[i]);	
		}
		
		area.setBounds(140,20,325,200);
		area.setFont(new Font("Sanserif",Font.ITALIC,25));
		area.setBackground(Color.LIGHT_GRAY);
		area.setEditable(false);
		field.setEditable(false);
		field.setBounds(140,228,325,20);
	    field.setFont(new Font("Sanserif",Font.BOLD,15));
	    field.setBackground(Color.LIGHT_GRAY);
		area_score.setBackground(Color.GREEN);
		area_score.setForeground(Color.BLACK);
		area_score.setFont(new Font("Sanserif",Font.BOLD,23));
		area_score.setBounds(20,120,95,90);
		area_score.setEditable(false);
	}
	
public class ascoltatore implements ActionListener {
		
		public void actionPerformed(ActionEvent e){
			String tmp= e.getActionCommand();
			
			if (tmp=="start"){
				field.setText("");
				area.setText("Player\n");
				player=1+(int)(MyMath.random()*12)+1;
				player=control(player);
				x=(int)(MyMath.random()*12)+1;
				x=control(x);
				player+=x;
				if (player==21) field.setText("21 Vittoria Grande Baldoria!");
				out=false;
			}
			
			if (tmp=="stand"){
				if (out!=true){
				x=(int)(MyMath.random()*12)+1;
				x=control(x);
				player+=x;
				if (player>20) { field.setText("Busted!");
				out=true;
				state_cpu+=1; area_score.setText("Player: "+state_player+"\n\nCpu: "+state_cpu);
				}
				else if (player==21) field.setText("BlackJack!");
				} else{JOptionPane.showMessageDialog(null,"push start!","",JOptionPane.ERROR_MESSAGE);}
			}
			if (tmp=="hit"){
				if (out!=true){
					out=true;
				area.setText(area.getText()+"\n\nCpu\n");
				cpu();
				} else{JOptionPane.showMessageDialog(null,"push start!","",JOptionPane.ERROR_MESSAGE);}
			}
		}

	}
	
	public int control(int p) { 

	if (p >=2 && p<=10) {area.setText(area.getText()+" "+p); return p;}
	if (p==1){ 
		area.setText(area.getText()+" A"); 
		cpu_aces+=1;		// CORRECTION: count cpu aces number  
		return 11;}
	else {
	if (p==13)area.setText(area.getText()+" K");
	else if (p==12)area.setText(area.getText()+" Q");
	else if (p==11)area.setText(area.getText()+" Q");
	return 10;
	}
	
}
	
	public void cpu(){ 
		cpu=(int)(MyMath.random()*12)+1;
		cpu=control(cpu);
		
		x=(int)(MyMath.random()*12)+1;
		x=control(x);
		cpu+=x;
		
		// CORRECTION: verifica se ci sono assi da svalutare per evitare di perdere ("busted")
		aceCorrection();
		
		while (cpu<player || cpu<17){
			x=(int)(MyMath.random()*12)+1;
			cpu+=control(x);
			
			// CORRECTION: verifica se ci sono assi da svalutare per evitare di perdere ("busted")
			aceCorrection();
		}
		if (cpu>21){
			field.setText("Win!"); state_player+=1; area_score.setText("Player: "+state_player+"\n\nCpu: "+state_cpu); } 
		else if (cpu!=player){
			field.setText("Lose!"); state_cpu+=1; area_score.setText("Player: "+state_player+"\n\nCpu: "+state_cpu); } 
		cpu=0;
		
	}
	
	// CORRECTION: se ci sono assi ancora da svalutare, cambia il valore di uno di essi da 11 a 1
	public void aceCorrection(){ 
		if(cpu>21 && cpu_aces>0){
			cpu-=10;
			cpu_aces-=1;
		}
	}

}